"""
测试Data__Utils功能
"""

from com.tools.Data_Utils import DataUtils


def test_data_utils():
    """
    测试数据工具类
    """
    print("测试真随机数生成:")
    
    # 测试生成真随机字符串
    random_string = DataUtils.generate_true_random_string(10)
    print(f"随机字符串 (10位): {random_string}")
    assert len(random_string) == 10
    
    # 测试使用自定义字符集生成真随机字符串
    custom_charset = "0123456789abcdef"
    random_hex_string = DataUtils.generate_true_random_string(8, custom_charset)
    print(f"十六进制随机字符串 (8位): {random_hex_string}")
    assert len(random_hex_string) == 8
    assert all(c in custom_charset for c in random_hex_string)
    
    # 测试生成真随机整数
    random_number = DataUtils.generate_true_random_number(1, 100)
    print(f"随机整数 (1-100): {random_number}")
    assert 1 <= random_number <= 100
    
    # 测试生成真随机浮点数
    random_float = DataUtils.generate_true_random_float(0.0, 100.0, 2)
    print(f"随机浮点数 (0-100): {random_float}")
    assert 0.0 <= random_float <= 100.0
    
    # 测试生成真随机字节串
    random_bytes = DataUtils.generate_true_random_bytes(16)
    print(f"随机字节串 (16字节): {random_bytes}")
    assert len(random_bytes) == 16
    
    # 测试生成真随机十六进制字符串
    random_hex = DataUtils.generate_true_random_hex(8)
    print(f"随机十六进制字符串: {random_hex}")
    assert len(random_hex) == 16  # token_hex生成的字符串长度是参数的2倍
    
    # 测试生成真随机URL安全字符串
    random_urlsafe = DataUtils.generate_true_random_urlsafe_string(12)
    print(f"随机URL安全字符串: {random_urlsafe}")
    
    print("\n测试汉字和混合字符串生成:")
    
    # 测试生成真随机汉字字符串
    random_chinese = DataUtils.generate_true_random_chinese_string(10)
    print(f"随机汉字字符串 (10位): {random_chinese}")
    assert len(random_chinese) == 10
    
    # 测试生成真随机混合字符串
    random_mixed = DataUtils.generate_true_random_mixed_string(15)
    print(f"随机混合字符串 (15位): {random_mixed}")
    assert len(random_mixed) == 15
    
    # 测试生成只包含数字和英文字母的混合字符串
    random_mixed_no_chinese = DataUtils.generate_true_random_mixed_string(
        15, include_digits=True, include_letters=True, include_chinese=False
    )
    print(f"随机混合字符串 (无汉字): {random_mixed_no_chinese}")
    assert len(random_mixed_no_chinese) == 15
    # 验证不包含汉字
    chinese_chars = DataUtils.CHINESE_CHARACTERS
    assert all(c not in chinese_chars for c in random_mixed_no_chinese)
    
    # 测试生成只包含汉字和英文字母的混合字符串
    random_mixed_no_digits = DataUtils.generate_true_random_mixed_string(
        15, include_digits=False, include_letters=True, include_chinese=True
    )
    print(f"随机混合字符串 (无数字): {random_mixed_no_digits}")
    assert len(random_mixed_no_digits) == 15
    # 验证不包含数字
    assert all(c not in '0123456789' for c in random_mixed_no_digits)
    
    print("\n测试随机抽取功能:")
    
    # 准备测试数据
    test_data = ['apple', 'banana', 'cherry', 'date', 'elderberry']
    print(f"测试数据: {test_data}")
    
    # 测试从数组中随机抽取一个元素
    random_choice = DataUtils.random_choice(test_data)
    print(f"随机抽取一个元素: {random_choice}")
    assert random_choice in test_data
    
    # 测试从数组中随机抽取多个元素（可重复）
    random_choices = DataUtils.random_choices(test_data, k=3)
    print(f"随机抽取3个元素: {random_choices}")
    assert len(random_choices) == 3
    assert all(item in test_data for item in random_choices)
    
    # 测试带权重的随机抽取
    weights = [10, 1, 1, 1, 1]  # apple权重更高
    weighted_choices = DataUtils.random_choices(test_data, k=5, weights=weights)
    print(f"带权重随机抽取5个元素: {weighted_choices}")
    assert len(weighted_choices) == 5
    assert all(item in test_data for item in weighted_choices)
    
    # 测试从数组中随机抽样（不重复）
    random_sample = DataUtils.random_sample(test_data, k=3)
    print(f"随机抽样3个不重复元素: {random_sample}")
    assert len(random_sample) == 3
    assert len(set(random_sample)) == 3  # 确保不重复
    assert all(item in test_data for item in random_sample)
    
    # 测试随机打乱数组
    shuffled = DataUtils.shuffle(test_data)
    print(f"随机打乱后的数组: {shuffled}")
    assert len(shuffled) == len(test_data)
    assert all(item in test_data for item in shuffled)
    
    print("\n测试随机数生成:")
    
    # 测试生成指定范围内的随机浮点数
    random_float = DataUtils.random_uniform(0.0, 10.0)
    print(f"随机浮点数 (0-10): {random_float}")
    assert 0.0 <= random_float <= 10.0
    
    # 测试生成高斯分布随机数
    gauss_random = DataUtils.random_gauss(0, 1)
    print(f"高斯分布随机数 (μ=0, σ=1): {gauss_random}")
    
    print("\n所有测试通过!")


if __name__ == "__main__":
    test_data_utils()